import pytest

from src.application.user.exceptions import UserIdAlreadyExist
from src.application.user.use_cases.create_user import CreateUser, CreateUserHandler
from src.domain.user.aggregates import UserAggregate
from src.domain.user.value_objects import TelegramUserId, UserId
from tests.mocks.uow import UnitOfWorkMock
from tests.mocks.user_repo import UserRepoMock


async def test_correct_user_create(
    uow: UnitOfWorkMock,
    user_repo: UserRepoMock,
) -> None:
    command = CreateUser(tg_id=1413248651, username="web2py")
    handler = CreateUserHandler(uow=uow)
    await handler(command=command)
    assert uow.commit_status is True
    assert uow.rollback_status is False


async def test_create_user_with_existing_tg_id(
    user_repo: UserRepoMock,
    uow: UnitOfWorkMock,
) -> None:
    command = CreateUser(tg_id=1413248651, username="web2py")
    handler = CreateUserHandler(uow=uow)
    existing_user = UserAggregate(UserId(value=0), tg_id=TelegramUserId(1413248651), username="test")
    user_repo.users[existing_user.tg_id] = existing_user

    with pytest.raises(UserIdAlreadyExist):
        await handler(command=command)

    assert uow.commit_status is False
    assert uow.rollback_status is False
